package com.yjt.fresh.adpater;

import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yjt.fresh.R;
import com.yjt.fresh.bean.BeanProduct;
import com.yjt.fresh.service.Utils;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.List;

import butterknife.BindView;

/**
 * 商品列表适配器
 * Created by mcc on 2018/6/10.
 */

public class AdpRvAtyProducts extends BaseQuickAdapter<BeanProduct, BaseViewHolder> {
    public AdpRvAtyProducts(int layoutResId, @Nullable List<BeanProduct> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, BeanProduct item) {
        ImageView ivProducts = helper.getView(R.id.ivAppItemAtyProducts);
        ImageView ivCart = helper.getView(R.id.ivAppItemAtyProductsCart);
        TextView tvTitle = helper.getView(R.id.tvAppItemAtyProductsTitle);
        TextView tvNewPrice = helper.getView(R.id.tvAppItemAtyProductsNewPrice);
        TextView tvOldPrice = helper.getView(R.id.tvAppItemAtyProductsOldPrice);
        TextView tvSaleQuantity = helper.getView(R.id.tvAppItemAtyProductsSaleQuantity);
        TextView tvDistance = helper.getView(R.id.tvAppItemAtyProductsDistance);

        Glide.with(mContext).load(item.getCOVER_IMG()).into(ivProducts);
        tvNewPrice.setText(item.getDISPLAY_SINGLE_PRICE());
        tvOldPrice.setText("¥  " + item.getDISPLAY_PRICE());
        tvTitle.setText(item.getTITLE());
        tvSaleQuantity.setText("已售   " + (item.getGROUP_COUNT() == null ? "0" : item.getGROUP_COUNT()));
        if (item.getSTORE_LATITUDE_LONGITUDE() != null) {
            String[] location = item.getSTORE_LATITUDE_LONGITUDE().split(",");
            double mLatitude = Double.valueOf(PreferenceManager.getInstance().getLatitude());
            double mLongitude = Double.valueOf(PreferenceManager.getInstance().getLongitude());
            if (mLatitude != 0 && mLongitude != 0) {
                try {
                    double distance = Utils.getDistance(mLongitude, mLatitude, Double.valueOf(location[0]), Double.valueOf(location[1]));
                    tvDistance.setText(distance + "km");
                } catch (NumberFormatException e) {
                    tvDistance.setText("0km");
                }
            }
        } else {
            tvDistance.setText("0km");
        }
    }
}
